package net.roading.mvc.control
{   
	
	import net.roading.mvc.commands.ICommand;
	import flash.utils.Dictionary;
 
   public class Controller
   {
     /**
      * Dictionary of event name to command class mappings
      */ 
      protected var commands : Dictionary = new Dictionary();

     /**
      * Registers a ICommand class with the Front Controller, against an event name
      * and listens for events with that name.
      *
      * <p>When an event is broadcast that matches commandName,
      * the ICommand class referred to by commandRef receives control of the
      * application, by having its execute() method invoked.</p>
      *
      * @param commandName The name of the event that will be broadcast by the
      * when a particular user gesture occurs, eg "login"
      *
      * @param commandRef An ICommand Class reference upon which execute()
      * can be called when the Front Controller hears an event broadcast with
      * commandName. Typically, this argument is passed as "LoginCommand" 
      * or similar.
      * 
      * @param useWeakReference A Boolean indicating whether the controller
      * should added as a weak reference to the CairngormEventDispatcher,
      * meaning it will eligibile for garbage collection if it is unloaded from 
      * the main application.
      */     
      public function addCommand( commandName : String, commandRef : Class, useWeakReference : Boolean = true ) : void
      {
         if( commands[ commandName ] != null )return;
            //throw new CairngormError( CairngormMessageCodes.COMMAND_ALREADY_REGISTERED, commandName );
         
         commands[ commandName ] = commandRef;
         Dispatcher.getInstance().addEventListener( commandName, executeCommand, false, 0, useWeakReference );
      }
      
     /**
      * Deregisters an ICommand class with the given event name from the Front Controller 
      *
      * @param commandName The name of the event that will be broadcast by the
      * when a particular user gesture occurs, eg "login"
      *
      */     
      public function removeCommand( commandName : String ) : void
      {
         if( commands[ commandName ] === null)return;
           // throw new CairngormError( CairngormMessageCodes.COMMAND_NOT_REGISTERED, commandName);  
         
         Dispatcher.getInstance().removeEventListener( commandName, executeCommand );
         commands[ commandName ] = null;
         delete commands[ commandName ]; 
      }
      
     /**
      * Executes the command
      */  
      protected function executeCommand( event : MvcEvent ) : void
      {
         var commandToInitialise : Class = getCommand( event.type );
         var commandToExecute : ICommand = new commandToInitialise();

         commandToExecute.execute( event );
      }
      
     /**
      * Returns the command class registered with the command name. 
      */
      protected function getCommand( commandName : String ) : Class
      {
         var command : Class = commands[ commandName ];
         
         if ( command == null )return null;
           // throw new CairngormError( CairngormMessageCodes.COMMAND_NOT_FOUND, commandName );
            
         return command;
      }      
   }   
}
